/*
 * Copyright (C) 2019-2020 Alibaba Group Holding Limited
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "drv_display.h"

#define MDISPLAY_DRIVER(dev)  ((mdisplay_driver_t*)(dev->drv))
#define MDISPLAY_VALID(dev) do { \
    if (device_valid(dev, "mdisp") != 0) \
        return -1; \
} while(0)

int mdisplay_config(aos_dev_t *dev, mdisplay_config_t *config)
{
    int ret;

    MDISPLAY_VALID(dev);

    device_lock(dev);
    ret = MDISPLAY_DRIVER(dev)->config(dev, config);
    device_unlock(dev);

    return ret;
}

int mdisplay_enable(aos_dev_t *dev)
{
    int ret;

    MDISPLAY_VALID(dev);

    device_lock(dev);
    ret = MDISPLAY_DRIVER(dev)->enable(dev);
    device_unlock(dev);

    return ret;
}

int mdisplay_disable(aos_dev_t *dev)
{
    int ret;

    MDISPLAY_VALID(dev);

    device_lock(dev);
    ret = MDISPLAY_DRIVER(dev)->disable(dev);
    device_unlock(dev);

    return ret;
}

int mdisplay_set_brightness(aos_dev_t *dev, int brightness)
{
    int ret;

    MDISPLAY_VALID(dev);

    device_lock(dev);
    ret = MDISPLAY_DRIVER(dev)->set_brightness(dev, brightness);
    device_unlock(dev);

    return ret;
}

int mdisplay_show(aos_dev_t *dev, uint8_t *buf, size_t len)
{
    int ret;

    MDISPLAY_VALID(dev);

    device_lock(dev);
    ret = MDISPLAY_DRIVER(dev)->show(dev, buf, len);
    device_unlock(dev);

    return ret;
}

int mdisplay_extled_set(aos_dev_t *dev, uint8_t idx, uint8_t val)
{
    int ret;

    MDISPLAY_VALID(dev);

    device_lock(dev);
    ret = MDISPLAY_DRIVER(dev)->extled_set(dev, idx, val);
    device_unlock(dev);

    return ret;
}


